package com.zycfc.zsf.cloud.core.domain;

import javax.servlet.*;
import com.zycfc.zsf.boot.redis.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import com.zycfc.zsf.cloud.core.domain.auth.*;
import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.util.json.*;
import javax.servlet.http.*;
import com.zycfc.zsf.boot.core.web.data.*;
import com.zycfc.zsf.boot.exception.*;
import org.springframework.util.*;
import java.io.*;
import java.util.*;
import org.slf4j.*;

public abstract class AbstractProviderCompont
{
    private static final Logger log;
    private static ThreadLocal<ServletRequest> requestThreadLocal;
    private static ThreadLocal<ServletResponse> responseThreadLocal;
    public static final String SUCCESS = "0000000";
    public static final String ERROR = "9999999";
    private static final String ERROR_MSG = "\u64cd\u4f5c\u5931\u8d25";
    private static final String ADMIN_NO = "999999";
    private static final String ADMIN_NAME = "admin";
    private static final String HTTP_METHOD_GET = "GET";
    @Autowired
    private IRedisOperater redisOperater;
    
    @ModelAttribute
    public void initHttpServlet(final HttpServletRequest request, final HttpServletResponse response) {
        AbstractProviderCompont.log.debug("*****ThreadLocal set success****");
        AbstractProviderCompont.requestThreadLocal.set((ServletRequest)request);
        AbstractProviderCompont.responseThreadLocal.set((ServletResponse)response);
    }
    
    public ZsfAuthUser getTokenUser() {
        final String token = this.request().getHeader("authToken");
        AbstractProviderCompont.log.info("token=" + token);
        if (StringUtils.isEmpty(token)) {
            final ZsfAuthUser tempUser = this.getAdminUser();
            return tempUser;
        }
        final String userJson = (String)this.redisOperater.get("gateway:token:" + token);
        if (StringUtils.isEmpty(userJson)) {
            AbstractProviderCompont.log.error("\u6839\u636etoken:{}\u4eceredis\u83b7\u53d6\u5230\u7684userJson\u4e3aNUll\uff0c\u5c06\u8fd4\u56de\u9ed8\u8ba4admin\u7528\u6237999999\uff01", (Object)token);
            final ZsfAuthUser tempUser2 = this.getAdminUser();
            return tempUser2;
        }
        final Map<String, Object> responseData = (Map<String, Object>)JsonUtils.jsonToMap(userJson);
        ZsfAuthUser user = null;
        final String code = String.valueOf(responseData.get("code"));
        if ("0000000".equals(code)) {
            final Object obj = responseData.get("data");
            final String objStr = JsonUtils.objectToJson(obj);
            user = (ZsfAuthUser)JsonUtils.jsonToObject(objStr, (Class)ZsfAuthUser.class);
            user.setUserId(user.getEmplyNo());
        }
        else {
            AbstractProviderCompont.log.error("\u6839\u636etoken:{}\u4eceredis\u83b7\u53d6\u5230\u7684userJson\u4e2dcode\u4e3a:{}\uff0c\u800c\u4e0d\u662f0000000\uff0c\u5c06\u8fd4\u56de\u9ed8\u8ba4admin\u7528\u6237999999\uff01", (Object)token, (Object)code);
            user = this.getAdminUser();
        }
        return user;
    }
    
    private ZsfAuthUser getAdminUser() {
        final ZsfAuthUser user = new ZsfAuthUser();
        user.setUserId("999999");
        user.setEmplyNo("999999");
        user.setName("admin");
        return user;
    }
    
    public String getTokenUserId() {
        final ZsfAuthUser user = this.getTokenUser();
        if (user == null) {
            return "999999";
        }
        return user.getUserId();
    }
    
    public void logOut() {
        final String token = this.request().getHeader("authToken");
        final String key = "gateway:token:" + token;
        if (this.redisOperater.exists(key)) {
            this.redisOperater.delete(key);
        }
    }
    
    public HttpServletRequest request() {
        return (HttpServletRequest)AbstractProviderCompont.requestThreadLocal.get();
    }
    
    public HttpServletResponse response() {
        return (HttpServletResponse)AbstractProviderCompont.responseThreadLocal.get();
    }
    
    public HttpSession session() {
        return this.request().getSession();
    }
    
    public <T> DataResponse<T> succeed(final T data) {
        final DataResponse<T> defaultResponse = (DataResponse<T>)DataResponse.succeed();
        defaultResponse.setData((Object)data);
        return defaultResponse;
    }
    
    public <T> DataResponse<T> succeed(final T data, final int current, final int pageSize, final long total, final int pages) {
        final DataResponse<T> defaultResponse = (DataResponse<T>)DataResponse.succeed();
        defaultResponse.setData((Object)data);
        final Page pagination = new Page(current, pageSize, total, (long)pages);
        defaultResponse.setPagination(pagination);
        return defaultResponse;
    }
    
    public <T> DataResponse<T> failed() {
        return (DataResponse<T>)DataResponse.builder().code("9999999").message("\u64cd\u4f5c\u5931\u8d25").build();
    }
    
    public <T> DataResponse<T> failed(final Exception e) {
        if (e instanceof ServiceException) {
            final ServiceException ex1 = (ServiceException)e;
            return this.failed((String)ex1.getCode().getValue(), ex1.getCode().getMessage(), (T)null);
        }
        if (e instanceof BaseException) {
            final BaseException ex2 = (BaseException)e;
            return this.failed((String)ex2.getCode().getValue(), ex2.getCode().getMessage(), (T)null);
        }
        return this.failed("9999999", e.getMessage(), (T)null);
    }
    
    public <T> DataResponse<T> failed(final String code, final String msg, final T data) {
        final DataResponse<T> response = (DataResponse<T>)DataResponse.builder().code(code).message(msg).build();
        response.setData((Object)data);
        return response;
    }
    
    public <T> DataResponse<T> failed(final String code, final String msg) {
        return this.failed(code, msg, (T)null);
    }
    
    public <T> DataResponse<T> failed(final IExceptionCode abstractEnum) {
        return this.failed((String)abstractEnum.getValue(), abstractEnum.getMessage(), (T)null);
    }
    
    public <T> DataResponse<T> failed(final IExceptionCode abstractEnum, final T data) {
        return this.failed((String)abstractEnum.getValue(), abstractEnum.getMessage(), data);
    }
    
    private String getBodyString(final HttpServletRequest request) {
        final String contentType = request.getContentType();
        String bodyString = "";
        if (org.apache.commons.lang3.StringUtils.isNotBlank((CharSequence)contentType) && (contentType.contains("multipart/form-data") || contentType.contains("x-www-form-urlencoded"))) {
            final Enumeration<String> pars = (Enumeration<String>)request.getParameterNames();
            while (pars.hasMoreElements()) {
                final String n = pars.nextElement();
                bodyString = bodyString + n + "=" + request.getParameter(n) + "&";
            }
            bodyString = (bodyString.endsWith("&") ? bodyString.substring(0, bodyString.length() - 1) : bodyString);
            return bodyString;
        }
        try {
            final byte[] byteArray = StreamUtils.copyToByteArray((InputStream)request.getInputStream());
            bodyString = new String(byteArray, "UTF-8");
        }
        catch (IOException e) {
            AbstractProviderCompont.log.error(e.getMessage());
        }
        return bodyString;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)AbstractProviderCompont.class);
        AbstractProviderCompont.requestThreadLocal = new ThreadLocal<ServletRequest>();
        AbstractProviderCompont.responseThreadLocal = new ThreadLocal<ServletResponse>();
    }
}
